﻿using ServerBase.Config;
using SvGame.Objects;
using static System.Net.Mime.MediaTypeNames;

namespace SvGame.Logic;

public static partial class GameLogic
{
    public static void BroadcastMsg<T>(this Room room, T msg) where T : gpb::IMessage
    {
        foreach (var player in room.DictPlayer.Values)
        {
            player.Send(msg);
        }
    }
    public static RoomInfo GetInfo(this Room room)
    {
        var info = new RoomInfo();
        info.Uid = room.Uid;
        foreach (var item in room.DictExit.Values)
        {
            if (GetRoom(item.RoomUid, out var roomexit))
            {
                var node = new RoomInfoExit();
                node.Dir = item.Dir;
                node.RoomUid = item.RoomUid;
                info.Exits[node.Dir] = node;
            }
        }
        foreach (var item in room.DictItem.Values)
        {
            var node = item.GetInfo();
            info.Items[node.Uid] = node;
        }
        foreach (var item in room.DictEquip.Values)
        {
            var node = item.GetInfo();
            info.Equips[node.Uid] = node;
        }
        foreach (var item in room.DictNpc.Values)
        {
            var node = item.GetInfo();
            info.Npcs[node.Uid] = node;
        }
        foreach (var item in room.DictPlayer.Values)
        {
            var node = item.GetRoomInfoPlayer();
            info.Players[node.Puid] = node;
        }
        return info;
    }
    public static void RoomInfo(this Player player, bool refreshDesc = true)
    {
        var rsp = new G2CNtfRoomnfo();

        rsp.RefreshDesc = refreshDesc;
        rsp.RoomInfo = player.Room.GetInfo();

        player.Send(rsp);
    }
    public static void BroadcastInfo(this Room room, bool refreshDesc = false)
    {
        var rsp = new G2CNtfRoomnfo();
        rsp.RefreshDesc = refreshDesc;
        rsp.RoomInfo = room.GetInfo();
        room.BroadcastMsg(rsp);
    }
}
